﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Trade.Controls;
using Trade.DAL;
using Trade.Model;

namespace Trade
{
    public partial class FrmDailyExpenses : Trade.Controls.RayForm
    {
        private DailyExpenses dailyExpenses = new DailyExpenses();
        private Utility.CRUD dataState = Utility.CRUD.None;
        private Common common = new Common();
        private DataTable dtDailyExpenses = null;

        public FrmDailyExpenses()
        {
            InitializeComponent();
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
            common.SetControlsState(this, false);

            btnOperatorGroup.btnNew.Click += new EventHandler(btnNew_Click);
            btnOperatorGroup.btnModify.Click += new EventHandler(btnModify_Click);
            btnOperatorGroup.btnDelete.Click += new EventHandler(btnDelete_Click);
            btnOperatorGroup.btnSave.Click += new EventHandler(btnSave_Click);
            btnOperatorGroup.btnCancel.Click += new EventHandler(btnCancel_Click);
            btnOperatorGroup.btnQuit.Click += new EventHandler(btnQuit_Click);
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            if (dataState != Utility.CRUD.None)
            {
                if (MessageBox.Show("当前数据还未保存,是否需要保存数据?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
                {
                    btnOperatorGroup.btnSave.Focus();
                    return;
                }
            }

            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            dataState = Utility.CRUD.None;
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
            common.SetControlsState(this, false);
            dgvDailyExpenses.Enabled = true;
            dgvDailyExpenses.Focus();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgvDailyExpenses.RowCount != 0)
            {
                if (MessageBox.Show("是否真的删除当前数据?", "删除数据", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
                {
                    dataState = Utility.CRUD.Delete;
                    common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
                    dgvDailyExpenses.Enabled = false;
                }
            }
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            dataState = Utility.CRUD.Update;
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
            DailyExpensesInfo dailyExpensesInfo = new DailyExpensesInfo();
            dailyExpensesInfo.RecordId = (int)dgvDailyExpenses.CurrentRow.Cells["RecordId"].Value;

            common.SetControlsState(this, true);
            dgvDailyExpenses.Enabled = false;
            txtBillNo.Enabled = false;
            txtAccount.Focus();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            dataState = Utility.CRUD.Insert;
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
            common.SetControlsState(this, true);
            dgvDailyExpenses.Enabled = false;

            txtBillNo.Text = "";
            txtBillNo.Focus();
        }

        private void SetDailyExpensesDataGridView()
        {
            dgvDailyExpenses.AllowUserToAddRows = false;
            dgvDailyExpenses.AllowUserToDeleteRows = false;
            dgvDailyExpenses.ReadOnly = true;

            dgvDailyExpenses.Columns[0].Width = 100;
            dgvDailyExpenses.Columns[0].Name = dtDailyExpenses.Columns["BillNo"].ColumnName.ToString();
            dgvDailyExpenses.Columns[0].HeaderText = "账单编号";
            dgvDailyExpenses.Columns[1].Width = 200;
            dgvDailyExpenses.Columns[1].Name = dtDailyExpenses.Columns["Account"].ColumnName.ToString();
            dgvDailyExpenses.Columns[1].HeaderText = "账户";
            dgvDailyExpenses.Columns[2].Width = 100;
            dgvDailyExpenses.Columns[2].Name = dtDailyExpenses.Columns["FeeSubjectNo"].ColumnName.ToString();
            dgvDailyExpenses.Columns[2].HeaderText = "费用项目编码";
            dgvDailyExpenses.Columns[3].Width = 100;
            dgvDailyExpenses.Columns[3].Name = dtDailyExpenses.Columns["FeeSubjectName"].ColumnName.ToString();
            dgvDailyExpenses.Columns[3].HeaderText = "费用项目";
            dgvDailyExpenses.Columns[4].Width = 100;
            dgvDailyExpenses.Columns[4].Name = dtDailyExpenses.Columns["Amount"].ColumnName.ToString();
            dgvDailyExpenses.Columns[4].HeaderText = "金额";
            dgvDailyExpenses.Columns[4].Frozen = true;
            dgvDailyExpenses.Columns[5].Width = 80;
            dgvDailyExpenses.Columns[5].Name = dtDailyExpenses.Columns["Department"].ColumnName.ToString();
            dgvDailyExpenses.Columns[5].HeaderText = "部门ID";
            dgvDailyExpenses.Columns[5].Visible = false;
            dgvDailyExpenses.Columns[6].Width = 100;
            dgvDailyExpenses.Columns[6].Name = dtDailyExpenses.Columns["Dept"].ColumnName.ToString();
            dgvDailyExpenses.Columns[6].HeaderText = "部门";
            dgvDailyExpenses.Columns[7].Width = 80;
            dgvDailyExpenses.Columns[7].Name = dtDailyExpenses.Columns["Employee"].ColumnName.ToString();
            dgvDailyExpenses.Columns[7].HeaderText = "经手人";
            dgvDailyExpenses.Columns[8].Width = 80;
            dgvDailyExpenses.Columns[8].Name = dtDailyExpenses.Columns["PayDate"].ColumnName.ToString();
            dgvDailyExpenses.Columns[8].HeaderText = "使用日期";
            dgvDailyExpenses.Columns[9].Width = 80;
            dgvDailyExpenses.Columns[9].Name = dtDailyExpenses.Columns["EntryDate"].ColumnName.ToString();
            dgvDailyExpenses.Columns[9].HeaderText = "录入日期";
            dgvDailyExpenses.Columns[10].Width = 150;
            dgvDailyExpenses.Columns[10].Name = dtDailyExpenses.Columns["Remark"].ColumnName.ToString();
            dgvDailyExpenses.Columns[10].HeaderText = "备注";
            dgvDailyExpenses.Columns[11].Width = 80;
            dgvDailyExpenses.Columns[11].Name = dtDailyExpenses.Columns["RecordId"].ColumnName.ToString();
            dgvDailyExpenses.Columns[11].HeaderText = "记录ID";
            dgvDailyExpenses.Columns[11].Visible = false;

            dgvDailyExpenses.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        }
    }
}
